﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using PlayMyLife.Definitions.HumanProperty;
using PlayMyLife.Definitions.StoryEvent;
using PlayMyLife.ModelInterface;

using System.IO;
using System.Collections.Generic;

namespace PlayMyLife.ModelLocal {
    public class LocalConfigManager : IConfigManager {
        public EventConfig EventConfig { get; private set; }
        public SkillConfig SkillConfig { get; private set; }

        public LocalConfigManager DeepCopy() {
            return new LocalConfigManager() {
                EventConfig = EventConfig.DeepCopy(),
                SkillConfig = SkillConfig.DeepCopy(),
            };
        }

        public EventStep GetEventByID(long id) {
            return EventConfig.events[id];
        }

        public bool InitFromText(string eventConfig, string skillConfig) {
            try {
                // EventConfig
                EventConfig = new EventConfig();
                var strReader = new StringReader(eventConfig);
                var reader = new JsonTextReader(strReader);
                var arr = JToken.ReadFrom(reader) as JObject;
                var events = arr["events"] as JArray;
                var eventSettings = JsonConvert.DeserializeObject<EventStep[]>(events.ToString());
                EventConfig.events = new Dictionary<long, EventStep>();
                foreach(var i in eventSettings) {
                    EventConfig.events.Add(i.id, i);
                }
                var argSettings = arr["args"] as JObject;
                EventConfig.args = new Dictionary<string, string>();
                foreach(var arg in argSettings) {
                    EventConfig.args.Add(arg.Key, arg.Value.ToString());
                }

                // SkillConfig
                SkillConfig = JsonConvert.DeserializeObject<SkillConfig>(skillConfig);

                // return
                reader.Close();
                strReader.Close();
                return EventConfig != null && EventConfig.events != null && SkillConfig != null;
            } catch(System.Exception) {
                return false;
            }
        }

        public string EventParseToText() {
            var eventData = new List<EventStep>(EventConfig.events.Count);
            foreach(var i in EventConfig.events) {
                eventData.Add(i.Value);
            }
            var events = new JArray(eventData.ToArray());

            var args = new JObject();
            foreach(var i in EventConfig.args) {
                var strReader = new StringReader(i.Value);
                var reader = new JsonTextReader(strReader);
                var arr = JToken.ReadFrom(reader) as JObject;
                args.Add(i.Key, arr);
                reader.Close();
                strReader.Close();
            }

            var root = new JObject(){
                { "events", events },
                { "args", args }
            };
            return root.ToString();
        }

        public string SkillParseToText() {
            return JsonConvert.SerializeObject(SkillConfig);
        }
    }
}
